package com.masq.agencija.pages;

import java.util.Collection;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.masq.agencija.beans.RentB;
import com.masq.agencija.interfaces.Stan;
import com.masq.agencija.interfaces.Rent;
import com.masq.agencija.interfaces.Agent;
import com.masq.agencija.interfaces.Client;
import com.masq.agencija.interfaces.AuxClient;
import com.masq.agencija.pages.Login;
import com.masq.agencija.pages.PregledRent;
import com.masq.agencija.pages.PregledError;
import com.masq.agencija.service.Stanovi;
import com.masq.agencija.service.Rents;
import com.masq.agencija.service.Agenti;
import com.masq.agencija.service.Clients;

public class UnosRenta {
	@SessionState
	private AuxClient user;

	private boolean userExists;

	public AuxClient getUser() {
		return user;
	}

	Object onActivate() {
		if (!userExists)
			return Login.class;
		return null;
	}

	
	@Persist
	private String rentClient;
	@Persist
	private String rentAgent;
	@Persist
	private String rentStan;

	
	@InjectPage
	private PregledRent viewRentPage;
	@InjectPage
	private PregledError viewErrorPage;

	
	@Inject
	private Stanovi stanovi;
	@Inject
	private Clients klijenti;
	@Inject
	private Agenti agenti;
	@Inject
	private Rents rents;

	
	public Collection<Stan> getStanovi() {
		return stanovi.retrieve();
	}

	
	public Collection<Client> getClients() {
		return klijenti.retrieve();
	}

	
	public Collection<Agent> getAgenti() {
		return agenti.retrieve();
	}

	@OnEvent(value = "submit", component = "rentInputForm")
	Object onFromSubmit() {

		Stan stan = null;
		Client klijent = null;
		Agent agent = null;

		try {
			klijent = klijenti.retrieveSingleClient(getRentClient());
			agent = agenti.retrieveSingleAgent(getRentAgent());
			stan = stanovi.retrieveSingleStan(getRentStan());
		} catch (Exception ex) {
			viewErrorPage.setPassedMessage(ex.toString());
			return viewErrorPage;
		}

		
		if (klijent == null) {
			viewErrorPage
					.setPassedMessage("Greska u prepoznavanju objekta Client u Map-i");
			return viewErrorPage;
		}
		if (agent == null) {
			viewErrorPage
					.setPassedMessage("Greska u prepoznavanju objekta Librarian u Map-i");
			return viewErrorPage;
		}
		if (stan == null) {
			viewErrorPage
					.setPassedMessage("Greska u prepoznavanju objekta Book u Map-i");
			return viewErrorPage;
		}

		Rent rentB = new RentB();
		rentB.executeRentCreation(klijent, agent, stan, rents);

		this.rentStan = "";
		this.rentAgent = "";
		this.rentClient = "";

		return viewRentPage;
	}

	
	public String getRentClient() {
		return rentClient;
	}

	public void setRentClient(String rentClient) {
		this.rentClient = rentClient;
	}

	public String getRentAgent() {
		return rentAgent;
	}

	public void setRentAgent(String rentAgent) {
		this.rentAgent = rentAgent;
	}

	public String getRentStan() {
		return rentStan;
	}

	public void setRentStan(String rentStan) {
		this.rentStan = rentStan;
	}

}
